package com.seeTools.next.controller.sys;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.seeTools.next.api.entity.sys.SysConfig;
import com.seeTools.next.common.core.PageQuery;
import com.seeTools.next.common.core.Result;
import com.seeTools.next.service.sys.SysConfigService;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * @Author: seeTools
 * @Date: 2024/5/7  10:10
 * @Version 1.0
 */
@RestController
@RequestMapping("/config")
public class SysConfigController {

    @Resource
    private SysConfigService sysConfigService;


    /**
     * 获取参数配置列表
     */
//    @SaCheckPermission("system:config:list")
    @GetMapping("/list")
    public Result<Page<SysConfig>> list(PageQuery<SysConfig> pageQuery) {
        Page<SysConfig> pageInfo = sysConfigService.findList(pageQuery);
        return Result.success(pageInfo);
    }

    //    @SaCheckPermission("system:config:export")
    @PostMapping("/export")
    public void export(HttpServletResponse response, SysConfig config) {
        List<SysConfig> list = sysConfigService.selectDataList(config);
    }

    /**
     * 根据参数编号获取详细信息
     */
//    @SaCheckPermission("system:config:query")
    @GetMapping(value = "/{configId}")
    public Result<SysConfig> getInfo(@PathVariable Long configId) {
        return Result.success(sysConfigService.getById(configId));
    }

    /**
     * 根据参数键名查询参数值
     */
    @GetMapping(value = "/configKey/{configKey}")
    public Result<SysConfig> getConfigKey(@PathVariable String configKey) {
        return Result.success(sysConfigService.findOne(SysConfig::getConfigKey, configKey));
    }

    /**
     * 新增参数配置
     */
//    @SaCheckPermission("system:config:add")
    @PostMapping
    public Result<Void> add(@Validated @RequestBody SysConfig config) {
        if (!sysConfigService.checkConfigKeyUnique(config)) {
            return Result.error("新增参数'" + config.getConfigName() + "'失败，参数键名已存在");
        }
        return Result.state(sysConfigService.save(config));
    }

    /**
     * 修改参数配置
     */
//    @SaCheckPermission("system:config:edit")
    @PutMapping
    public Result<Void> edit(@Validated @RequestBody SysConfig config) {
        if (!sysConfigService.checkConfigKeyUnique(config)) {
            return Result.error("修改参数'" + config.getConfigName() + "'失败，参数键名已存在");
        }
        return Result.state(sysConfigService.updateOnLock(config));
    }

    /**
     * 删除参数配置
     */
//    @SaCheckPermission("system:config:remove")
    @DeleteMapping("/{configIds}")
    public Result<Void> remove(@PathVariable Long[] configIds) {
        return Result.state(sysConfigService.removeById(configIds));
    }

    /**
     * 根据key修改配置
     *
     * @param sysConfig
     * @return
     */
    @PutMapping("/updateByKey")
    public Result<Void> updateByKey(@RequestBody SysConfig sysConfig) {
        return Result.state(sysConfigService.updateByKey(sysConfig));
    }

}
